<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.cooId"
        filterable
        class="filter-item"
        clearable
        placeholder="请选择加油站"
        @keyup.enter.native="handleFilter"
      >
        <el-option
          v-for="item in cooperation"
          :label="item.company"
          :value="item.id"
        />
      </el-select>
      <el-date-picker
        v-model="listQuery.startDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择开始日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.endDay"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择结束日期时间"
        style="width: 200px;"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate(2)"
      >
        对账
      </el-button>
    </div>

    <el-table
      :key="key"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        prop="id"
        align="center"
        width="50"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="cooCompany"
        label="油站名称"
        align="center"
        width=""
      />
      <el-table-column
        prop="cooPhone"
        label="油站电话"
        align="center"
        width="100"
      />
      <el-table-column
        prop="earlyTime"
        label="期初时间"
        align="center"
        width="140"
      />
      <el-table-column
        prop="endTime"
        label="期末时间"
        align="center"
        width="140"
      />
      <el-table-column
        prop="earlyMoney"
        label="期初(元)"
        align="center"
        width="80"
      />
      <el-table-column
        prop="tranMoeny"
        label="期间交易(元)"
        align="center"
        width="80"
      />
      <el-table-column
        prop="rechMoney"
        label="期间充值(元)"
        align="center"
        width="80"
      />
      <el-table-column
        prop="endMoney"
        label="期末(元)"
        align="center"
        width="80"
      />
      <el-table-column
        prop="inputEndMoney"
        label="当前实际余额(元)"
        align="center"
        width="80"
      />

      <el-table-column
      prop="state"
      label="状态"
      align="center"
      width=""
      >
      <template slot-scope="scope">
      {{ scope.row.state | filtersTextChange }}
      </template>
      </el-table-column>
      <!--<el-table-column-->
      <!--label="操作"-->
      <!--align="center"-->
      <!--width="260px"-->
      <!--class-name="small-padding fixed-width"-->
      <!--fixed="right"-->
      <!--&gt;-->
      <!--<template slot-scope="scope">-->
      <!--<el-button-->
      <!--type="primary"-->
      <!--size="mini"-->
      <!--@click="handleUpdate(scope.row)"-->
      <!--&gt;编辑-->
      <!--</el-button>-->

      <!--&lt;!&ndash; <router-link :to="'/bankCard/'+scope.row.id"> &ndash;&gt;-->
      <!--<el-button-->
      <!--type="warning"-->
      <!--size="mini"-->
      <!--@click="detail(scope.row)"-->
      <!--&gt;详情-->
      <!--</el-button>-->
      <!--&lt;!&ndash; </router-link> &ndash;&gt;-->

      <!--</template>-->
      <!--</el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      top="2vh"
      width="680px"
    >
      <el-form
        ref="temp"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width:550px; margin-left:50px;"
      >

        <el-form-item
          label="加油站"
          prop="cooId"
        >

          <el-select
            v-model="temp.cooId"
            filterable
            class="filter-item"
            clearable
            placeholder="请选择加油站"
            style="width:440px"
          >
            <el-option
              v-for="item in cooperation"
              :label="item.company"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          label="金额"
          prop="inputEndMoney"
        >
          <el-input v-model="temp.inputEndMoney"/>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button
          type="primary"
          @click="createData()"
        >
          {{ $t('table.confirm') }}
        </el-button>
      </div>

    </el-dialog>

  </div>
</template>
<script>
  import permission from "@/directive/permission/index.js"; // 权限判断指令
  import checkPermission from "@/utils/permission"; // 权限判断函数

  import {
    getOilSupplyList,
    createOilSupply,
    getallAgent,
    business,
    getPageCooVerify,
    cooperationAll,
    addCooVerify
  } from "@/api/v1";
  import {supplyUnBind} from "@/api/user";

  import waves from "@/directive/waves"; // Waves directive
  import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
  import {parseTime} from "@/utils";
  import addImgall from "@/components/img";
  import provinceData from "@/components/province";

  export default {
    name: "agentVerify2",

    directives: {waves, permission},
    components: {Pagination, addImgall, provinceData},
    data() {
      return {
        editPhone: true,
        imageUrl: "",
        tableData: [],
        allAgent: [],
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        cooperation: [],
        listQuery: {
          page: 1,
          limit: 20,
          sort: "+id",
          cooId: "",
          startDay: "",
          endDay: ""
        },
        key: 1, // table key
        dialogFormVisible: false,
        rules: {
          inputEndMoney: [
            {required: true, message: "请输入对账金额", trigger: "blur"}
          ],
          cooId: [{required: true, message: "请选择加油站", trigger: "change"}]
        },
        dialogStatus: "",
        textMap: {
          update: "编辑",
          create: "新增"
        },
        temp: {
          inputEndMoney: "",
          cooId: ""
        },
        isAdmin: true
      };
    },
    watch: {
      checkboxVal(valArr) {
        this.formThead = this.formTheadOptions.filter(
          i => valArr.indexOf(i) >= 0
        );
        this.key = this.key + 1; // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
      }
    },

    created() {
      this.getCooperationAll();

      const id = this.$route.query.id;
      this.listQuery.cooperation = id;
      console.log(id);
      this.getList();
    },
    filters: {
      filtersTextChange: function (state) {
        switch (state) {
          case 1:
            return "账目正确";
          case 2:
            return "账目错误";
        }
      }
    },
    methods: {
      getCooperationAll() {
        cooperationAll().then(res => {
          this.cooperation = res.data;
        });
      },
      resetTemp() {
        this.temp.inputEndMoney = "";
        this.temp.cooId = "";
      },
      handleCreate(role) {
        this.resetTemp();
        console.log(this.temp);
        this.dialogStatus = "create";
        this.dialogFormVisible = true;
        this.editPhone = false;
        this.$nextTick(() => {
          this.$refs["temp"].clearValidate();
        });
      },
      createData() {
        this.$refs["temp"].validate(valid => {
          if (valid) {
            addCooVerify(this.temp).then(res => {
              // this.list.unshift(this.temp);
              this.dialogFormVisible = false;
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
              this.getList();
            });
          }
        });
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row); // copy obj
        console.log(row);
        this.dialogStatus = "update";
        this.dialogFormVisible = true;
        this.$refs.provinceData.getCityData();
        this.editPhone = true;
        this.$nextTick(() => {
          this.$refs["temp"].clearValidate();
        });
      },
      getList() {
        this.listLoading = true;
        getPageCooVerify(this.listQuery).then(res => {
          this.tableData = res.data.records;
          this.total = Number(res.data.total);
          console.log(this.tableData);
        });
      },
      getList2() {
        this.listLoading = true;
        getallAgent(this.listQuery).then(res => {
          this.allAgent = res.data;
          console.log(this.allAgent);
        });
      },
      getCooperationAll() {
        cooperationAll().then(res => {
          this.cooperation = res.data;

          // Just to simulate the time of the request
          setTimeout(() => {
            this.listLoading = false;
          }, 1.5 * 1000);
        });
      },
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      }
    }
  };
</script>

